package geppetto.main;


import geppetto.cat.corpus.BilingualCorpus;
import geppetto.phraseHMM.WordTrie;
import geppetto.phraseTable.phrase.Phrase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Properties;



public class ConvertIDs {
	protected static Properties _properties = new Properties();
	static{
		_properties.put("size", "30000");
		_properties.put("maxSentenceSize", "999");
		_properties.put("maxSourcePhraseSize", "3");
		_properties.put("maxTargetPhraseSize", "3");
		_properties.put("threshold", "0.3");
	}
	protected static String corpusDescription;
	protected static int size; 
	protected static int maxSentenceSize; 
	protected static int maxSourcePhraseSize; 
	protected static int maxTargetPhraseSize; 
	protected static String input;
	protected static String output;
	protected static String encoding;
	
	protected static BilingualCorpus corpus;

	protected static void loadProperties(String file){
		try {
			_properties.load(new FileInputStream(file));
		} catch (IOException e) {
			throw new AssertionError("Wrong properties file " + file);
		}
	}
	protected static void initArguments(){
		corpusDescription = _properties.getProperty("corpusDescription");
		size = Integer.parseInt(_properties.getProperty("size")); 
		maxSentenceSize = Integer.parseInt(_properties.getProperty("maxSentenceSize")); 
		maxSourcePhraseSize = Integer.parseInt(_properties.getProperty("maxSourcePhraseSize")); 
		maxTargetPhraseSize = Integer.parseInt(_properties.getProperty("maxTargetPhraseSize")); 
	}
	
	protected static void printArguments(){
		System.out.println("----General Parameters----");
		System.out.println("Corpus: " + corpusDescription);
		System.out.println("Size: " + size);
		System.out.println("Max Sentence size: " + maxSentenceSize);
		System.out.println("Max Source Phrase Size: " + maxSourcePhraseSize);
		System.out.println("Max Target Phrase Size: " + maxTargetPhraseSize);
		System.out.println("output: " + output);
		System.out.println("encoding: " + encoding);
	}
	protected static void loadCorpus() throws IOException{
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(corpusDescription));
		} catch (IOException e) {
			throw new AssertionError("Wrong properties file " + corpusDescription);
		}
		corpus = new BilingualCorpus();
		corpus.loadFromFileDescription(corpusDescription, size, maxSentenceSize);
		corpus.initialize();
		
	}
	
	public static void main(String[] args) throws Exception{
		loadProperties(args[0]);
		input = args[1];
		output = args[2];
		encoding = args[3];
		
		initArguments();
		printArguments();
				
		loadCorpus();
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input), encoding));
		PrintStream writer = new PrintStream(new File(output), encoding);
		while(reader.ready()){
			String line = reader.readLine();
			String[] extract = line.split(" \\|\\|\\| ");
			int[] source = convertStringArrayToIntArray(extract[0].split("\\s+"));
			int[] target = convertStringArrayToIntArray(extract[1].split("\\s+"));
			String entry = Phrase.getSourceString(corpus, source) + " ||| " + Phrase.getTargetString(corpus, target) + " ||| " + extract[2];
			if(extract.length > 2){
				for(int i = 3; i<extract.length; i++){
					entry += " ||| " + extract[i];
				}
			}
			writer.println(entry);
		}
	}

	public static int[] convertStringArrayToIntArray(String[] s){		
		int size = s.length;
		int[] ret = new int[size];
		for(int i = 0; i < size; i++){
			ret[i] = Integer.parseInt(s[i]);
		}
		return ret;
	}
}
